﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using eDoc.BO;
using System.Diagnostics;
namespace eDoc.Controls
{
    public partial class ucUpload : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btExtract_Click(object sender, EventArgs e)
        {
            //try
            //{
                string strPath = Server.MapPath("\\File\\FilePlist");
                DirectoryInfo direct = new DirectoryInfo(strPath);
                DirectoryInfo[] directoryList = direct.GetDirectories();
                FileInfo[] fileList;
  
                long total = 0;
                foreach (DirectoryInfo directInfo in directoryList)
                {
                    fileList = directInfo.GetFiles();
                    foreach (FileInfo file in fileList)
                    {
                        /*
                        if (file.Name.EndsWith(".ipa"))
                        {
                            Common.UnZipFilesNoEnc(file.FullName, file.Name, strPath);
                        }
                        */

                        if (file.Name.EndsWith(".plist"))
                        {
                        }
                    }
                }

                
            fileList = direct.GetFiles();
            foreach (FileInfo file in fileList)
            {
                if (file.Name.EndsWith(".ipa"))
                {
                    //Common.UnZipFilesNoEnc(file.FullName, file.Name, strPath);
                    total++;
                }
            }
            

                ltrNote.Text = "Sinh plist thanh cong!";
            
            /*
            fileList = direct.GetFiles();
            foreach (FileInfo file in fileList)
            {
                if (file.Name.EndsWith(".ipa"))
                {
                    Common.UnZipFilesNoEnc(file.FullName, file.Name, strPath);
                    total++;
                }
            }
            */
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            string strPath = Server.MapPath(txtDir.Text.Trim());
            DirectoryInfo direct = new DirectoryInfo(strPath);
            DirectoryInfo[] directoryList = direct.GetDirectories();
            FileInfo[] fileList;
            long total = 0;
            foreach (DirectoryInfo directInfo in directoryList)
            {
                fileList = directInfo.GetFiles();
                foreach (FileInfo file in fileList)
                {
                    if (file.Name.EndsWith(".ipa"))
                    {
                        //Common.UnZipFilesEnc(file.FullName, file.Name, strPath);
                    }
                }
            }


            fileList = direct.GetFiles();
            foreach (FileInfo file in fileList)
            {
                if (file.Name.EndsWith(".ipa"))
                {
                    //Common.UnZipFilesEnc(file.FullName, file.Name, strPath);
                    total++;
                }
            }


            ltrNote.Text = "Sinh plist thanh cong!";
            
        }
        string folderUnrar = "\\File\\FileUnrar";
        string folderPlist = "\\File\\FilePlist";
        string folderIPA = "\\File\\FileIPA";
        string folderInfo = "\\File\\FileInfo";
        string dirPlist ="";
        protected void btUnrar_Click(object sender, EventArgs e)
        {
           
            string strPath = Server.MapPath(txtDir.Text.Trim());
            DirectoryInfo direct = new DirectoryInfo(strPath);
            DirectoryInfo[] directoryList = direct.GetDirectories();
            FileInfo[] fileList;
            
            string strFileInfoPlist = "";    
            foreach (DirectoryInfo directInfo in directoryList)
            {
                fileList = directInfo.GetFiles();
                foreach (FileInfo file in fileList)
                {
                    if (file.Name.EndsWith(".ipa"))
                    {
                        string fileNamePlist = Common.Plist.PathFileInfoPlist(file.FullName);
                        int fileSize = (int)file.Length;
                        //string pathFileInfoPlist = Server.MapPath(folderUnrar) + "\\" + Common.Plist.PathFileInfoPlist(file.FullName);
                        string pathFileInfoPlist = Server.MapPath(folderUnrar) + "\\" + fileNamePlist;
                        dirPlist = Server.MapPath(folderPlist) + "\\" + file.Name.Substring(0, file.Name.IndexOf(".ipa"));

                        if (!Directory.Exists(dirPlist))
                            Directory.CreateDirectory(dirPlist);

                        if (!File.Exists(pathFileInfoPlist))
                        {
                            //Winrar(Server.MapPath(txtDir.Text) + "\\" + file, Server.MapPath(folderUnrar));
                            Winrar(Server.MapPath(folderIPA) + "\\" + file.Name.Substring(0, file.Name.IndexOf(".ipa")) + "\\" + file, Server.MapPath(folderUnrar));
                            strFileInfoPlist = Common.Plist.ConvertSavePlist(pathFileInfoPlist, dirPlist);
                        }

                        string urlInstall = Common.UrlInstall + folderIPA.Replace("\\","/") + "/" + file.Name.Substring(0, file.Name.IndexOf(".ipa")) + "/" + file;
                        //Common.Plist.ReadSavePlist(strFileInfoPlist, Server.MapPath(folderInfo), urlInstall, fileSize, fileName.Substring(0, fileName.IndexOf(".ipa")));
                    }
                }
            }          

        }


        private void Winrar(string strDirAPI, string strDirUnrar)
        {
            try
            {
                ProcessStartInfo startInfo = new ProcessStartInfo("WinRAR.exe");
                startInfo.WindowStyle = ProcessWindowStyle.Maximized;
                startInfo.Arguments = "x " + strDirAPI + " " + strDirUnrar;
                using (Process exeProcess = Process.Start(startInfo))
                {
                    exeProcess.WaitForExit();
                }
            }
            catch (Exception ex)
            {
                ltrNote.Text = ex.Message;
            }
        }

        private void SaveInfoPlist(string pathFilePlist, string forlderSavePlist)
        {

        }
    }
}